package com.itheima.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-10 11:01
 **/
@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页查询 + 条件查询
     *
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result queryStudentList(StudentQueryParam studentQueryParam) {
        PageResult<Student> pageResult = studentService.queryStudentList(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        Integer num = studentService.addStudent(student);
        if (num >= 1) {
            log.info("新增成功！");
            return Result.success();
        } else
            return Result.error("新增失败");
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryStudentById(@PathVariable Integer id) {
        Student student = studentService.queryStudentById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        log.info("修改学生信息！");
        Integer num = studentService.updateStudent(student);
        if (num >= 1) {
            log.info("修改成功！");
            return Result.success();
        } else
            return Result.error("修改失败");
    }

    /**
     * 批量删除学生
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delStudentByIds(@PathVariable List<Integer> ids) {
        log.info("删除id为{}学生信息！", ids);
        Integer num = studentService.deleteStudentByIds(ids);
        if (num >= 1) {
            log.info("删除成功！");
            return Result.success();
        } else
            return Result.error("删除失败");
    }

    /**
     * 学生违纪扣分
     *
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolationStu(@PathVariable Integer id, @PathVariable Short score) {
        studentService.updateViolationStu(id, score);
        return Result.success();
    }
}
